﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Renren.Helpers;
using Renren.Models;

namespace Renren.Services
{
    public static class FriendService
    {
        private static int page = 1;
        private static int count = 20;

        public static void SearchFriends(
            ObservableCollection<FriendModel> friendList,
            string searchString,
            RequestType requestType = RequestType.GetInit,
            Action callback = null)
        {
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "friends.search");
            parameters.Add("name", searchString);
            if (requestType == RequestType.GetInit)
            {
                page = 1;
                count = 20;
            }
            else
            {
                count = 40;
            }
            parameters.Add("page", page.ToString());
            parameters.Add("count", count.ToString());
            Service.SendRequest(
                parameters,
                result =>
                {
                    if (requestType == RequestType.GetInit)
                    {
                        friendList.Clear();
                    }
                    var root = XElement.Parse(result);
                    foreach (var element in root.Elements("friends", "friend"))
                    {
                        if (element.GetValue<int>("isFriend") == 1)
                        {
                            var friend = new FriendModel();
                            friend.Name = element.GetValue("name");
                            friend.UserId = element.GetValue<long>("id");
                            friend.TinyUrl = element.GetValue("tinyurl");
                            friendList.Add(friend);
                        }
                        page++;
                        if (callback != null)
                        {
                            callback();
                        }
                    }
                });
        }

        public static void GetFriends(
            ObservableCollection<FriendModel> friendList,
            RequestType requestType = RequestType.GetInit,
            Action callback = null)
        {
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "friends.getFriends");
            if (requestType == RequestType.GetInit)
            {
                page = 1;
                count = 20;
            }
            else
            {
                count = 40;
            }
            parameters.Add("page", page.ToString());
            parameters.Add("count", count.ToString());
            Service.SendRequest(
                parameters,
                result =>
                {
                    if (requestType == RequestType.GetInit)
                    {
                        friendList.Clear();
                    }
                    var root = XElement.Parse(result);
                    foreach (var element in root.Elements())
                    {
                        var friend = new FriendModel();
                        friend.Name = element.GetValue("name");
                        friend.UserId = element.GetValue<long>("id");
                        friend.HeadUrl = element.GetValue("headurl");
                        friend.TinyUrl = element.GetValue("tinyurl");
                        friendList.Add(friend);
                    }
                    page++;
                    if (callback != null)
                    {
                        callback();
                    }
                });
        }
    }
}
